#!/bin/bash
#sh app_info_out_update.sh -appInfoF "${FILE_PATH}" -p "${PlatformType}" -b "${BRANCH}" -pt "${PackageTargetType}" -pn "${PackageNetworkType}" -d "${ChangeLog}" -v $VERSION -bd $BUILD
#sh app_info_out_update.sh -appInfoF "app_info.json" -p iOS -b dev_all -pt pgyer -pn preproduct -d "更新说明略\n分支信息:\ndev_fix:功能修复" -v 1.0.0 -bd 11041000


echo "===============进入脚本:$0"


# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
# bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
# bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}
#echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"
qbase_json_file_check_script_path="$(sh ${qscript_path_get_filepath} qbase json_file_check)"

# 包来源分支
# shell 参数具名化
show_usage="args: [-appInfoF, -p , -b , -pt , -pn , -d, -v , -bd , -supPgyer, -supCos, -supAppStore, -np]\
                                  [--app-info-json-file=, --platformType=, --feature-branch=, --package_target_type=, --package_network_type=,\
                                  --update-description=, --version=, --build=,\
                                  --notificate-forPeople=]"

while [ -n "$1" ]
do
        case "$1" in
                -appInfoF|--app-info-json-file) FILE_PATH=$2; shift 2;;
                -p|--platformType) PlatformType=$2; shift 2;;
                -b|--feature-branch) BRANCH=$2; shift 2;;
                -pt|--package_target_type) PackageTargetType=$2; shift 2;;
                -pn|--package_network_type) PackageNetworkType=$2; shift 2;;
                -d|--update-description) ChangeLog=$2; shift 2;;
                -v|--version) VERSION=$2; shift 2;;
                -bd|--build) BUILD=$2; shift 2;;
                -np|--notificate-forPeople) NotificatePeople=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

JQ_EXEC=`which jq`

if [ ! -f "${FILE_PATH}" ];then
    echo "$0 ❌:您的$PWD/${FILE_PATH}文件不存在，请检查！"
    exit_script
fi

sh ${qbase_json_file_check_script_path} -checkedJsonF "${FILE_PATH}"


ShortBranceName=${BRANCH##*/}
echo "------ShortBranceName=$ShortBranceName"
if [ -n "$ShortBranceName" ]; then
    sed -i '' "s/package unknow brance/${ShortBranceName}/g" ${FILE_PATH}
fi

echo "------PlatformType:${PlatformType}"
echo "------PackageTargetType:${PackageTargetType}"
echo "------PackageNetworkType:${PackageNetworkType}"
if [ -n "$PlatformType" ]; then
    sed -i '' "s/package unknow platform/${PlatformType}/g" ${FILE_PATH}
fi
if [ -n "$PackageTargetType" ]; then
    sed -i '' "s/package unknow target/${PackageTargetType}/g" ${FILE_PATH}
fi
if [ -n "$PackageNetworkType" ]; then
    sed -i '' "s/package unknow env/${PackageNetworkType}/g" ${FILE_PATH}
fi

# 版本号version+build/VersionCode
echo "------VERSION:${VERSION}"
echo "------BUILD:${BUILD}"
if [ -n "$VERSION" ]; then
    sed -i '' "s/package unknow version/${VERSION}/g" ${FILE_PATH}
fi
if [ -n "$BUILD" ]; then
    sed -i '' "s/package unknow buildNumber/${BUILD}/g" ${FILE_PATH}
fi
# 包创建时间
mmddHHMM=$(date "+%m.%d")" "$(date "+%H:%M") # 02.21 1506
#echo "------mmddHHMM:${mmddHHMM}"
if [ -n "$FILE_PATH" ]; then
    sed -i '' "s/package unknow time/${mmddHHMM}/g" ${FILE_PATH}
fi

# 更新说明(可能有换行，需要使用此方法来兼容各种斜杠\及换行\n符等)
if [ -n "$ChangeLog" ]; then
    #sed -i '' "s/package unknow update/${ChangeLog}/g" ${FILE_PATH}
    sh ${qbase_update_json_file_singleString_script_path} -jsonF ${FILE_PATH} -k 'package_update_des' -v "${ChangeLog}"
fi
# 默认通知@人群
if [ -n "$NotificatePeople" ]; then
    sed -i '' "s/用户1#用户2#用户3#用户4/${NotificatePeople}/g" ${FILE_PATH}
fi

# 包类型的详细描述
if [ "${ShortBranceName}" == "master" -o "${ShortBranceName}" == "development" -o "${ShortBranceName}" == "dev_publish_out" ] ; then
    PackageFeatureType="formal"
elif [ "${ShortBranceName}" == "dev_publish_in" ] ; then
    PackageFeatureType="inner"
else
    PackageFeatureType="test"
fi

if [ "${PackageNetworkType}" == "develop1" ] ; then
    PackageTargetNetworkDes='开发包'
elif [ "${PackageNetworkType}" == "develop2" ] ; then
    PackageTargetNetworkDes='开发包'
elif [ "${PackageNetworkType}" == "test1" ] ; then
    PackageTargetNetworkDes='测试包'
    MentionedList[0]="@all"
elif [ "${PackageNetworkType}" == "preproduct" ] ; then
    PackageTargetNetworkDes='预生产包'
    MentionedList[0]="@all"
elif [ "${PackageNetworkType}" == "product" ] ; then
    if [ "${PackageTargetType}" == "formal" ] ; then
        if [ "${PlatformType}" == "Android" ] ; then
            PackageTargetNetworkDes="待加固成AppStore渠道 生产包"
        else
            PackageTargetNetworkDes="AppStore 生产包"
        fi
        
    elif [ "${PackageTargetType}" == "inner" ] ; then
        if [ "${PlatformType}" == "Android" ] ; then
            PackageTargetNetworkDes="内测于铂爵大楼(官网) 生产包"
        else
            PackageTargetNetworkDes="内测于铂爵大楼(官网+TestFlight) 生产包"
        fi
        
    else
        PackageTargetNetworkDes="内测于蒲公英 生产包"
    fi
    
    MentionedList[0]="@all"
else
    PackageTargetNetworkDes="${PackageTargetType}${PackageNetworkType}包"
fi

echo "------PackageTargetNetworkDes=${PackageTargetNetworkDes}"
if [ -n "$PackageTargetNetworkDes" ]; then
    sed -i '' "s/package unknow description/${PackageTargetNetworkDes}/g" ${FILE_PATH}
fi
#PackageTargetNetworkDes=$(cat $FILE_PATH | ${JQ_EXEC} .package_result | ${JQ_EXEC} .package_description | sed 's/\"//g')
#echo "PackageTargetNetworkDes=${PackageTargetNetworkDes}"

